package com.gxnzd.communityelderly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.entity.Forum;
import com.gxnzd.communityelderly.entity.PostLike;
import com.gxnzd.communityelderly.mapper.ForumMapper;
import com.gxnzd.communityelderly.service.PostLikeService;
import com.gxnzd.communityelderly.mapper.PostLikeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author 23234
* @description 针对表【post_like】的数据库操作Service实现
* @createDate 2024-11-24 15:12:14
*/
@Service
public class PostLikeServiceImpl extends ServiceImpl<PostLikeMapper, PostLike>
    implements PostLikeService{

    @Resource
    private PostLikeMapper postLikeMapper;

    @Resource
    private ForumMapper forumMapper;

    @Override
    public R setDropLike(boolean isLike, Integer postId, Integer userId, Integer Type,Integer criticizeId) {
        LambdaQueryWrapper<PostLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PostLike::getUserId,userId)
                .eq(PostLike::getType,Type);
        PostLike like = new PostLike();
        //判断是评论还是帖子
        if(Type == 1){
            wrapper.eq(PostLike::getPostId,postId);
            //针对帖子赞的总数进行操作
            Forum forum = forumMapper.selectById(postId);
            //判断点赞
            if(!isLike){
                //点赞了就取消点赞
                postLikeMapper.delete(wrapper);
                forum.setLikeNum(forum.getLikeNum() - 1);
                forumMapper.updateById(forum);
            }else{
                //没点赞就点赞
                like.setPostId(postId);
                like.setType(1);
                like.setUserId(userId);
                postLikeMapper.insert(like);
                forum.setLikeNum(forum.getLikeNum() + 1);
                forumMapper.updateById(forum);
            }

        }else {
            wrapper.eq(PostLike::getCriticizeId,criticizeId);
            //判断点赞
            if(!isLike){
                //点赞了就取消点赞
                postLikeMapper.delete(wrapper);
            }else{
                //没点赞就点赞
                like.setCriticizeId(criticizeId);
                like.setType(2);
                like.setUserId(userId);
                postLikeMapper.insert(like);
            }
        }

        return R.success("操作成功");
    }
}




